﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using QuTouTiaoAssist.Entity;

namespace QuTouTiaoAssist.Util
{
    class ConfigUtil
    {
        public static void SaveUserInfo(string nickName, 
                                        string memberToken, 
                                        string mobile, 
                                        string memberId, 
                                        string teacherId)
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (!config.AppSettings.Settings.AllKeys.Contains(memberId))
            {
                config.AppSettings.Settings.Add(memberId,string.Empty);
            }
            // 昵称,token,手机号,id,师傅id
            config.AppSettings.Settings[memberId].Value = 
                string.Format("{0},{1},{2},{3},{4}", nickName, memberToken,mobile,memberId,teacherId);
            config.Save();
        }

        public static List<QttMemberInfoLite> GetUserInfos()
        {
            var list = new List<QttMemberInfoLite>();

            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            for (int i = 0; i < config.AppSettings.Settings.AllKeys.Length; i++)
            {
                var key = config.AppSettings.Settings.AllKeys[i];
                var value = config.AppSettings.Settings[key].Value;
                if (value.Contains(","))
                {
                    // 用户信息
                    var values = value.Split(',');
                    list.Add(new QttMemberInfoLite()
                    {
                        MemberId = key,
                        NickName = values[0],
                        Token = values[1],
                        Mobile = values.Length>=3?values[2]:"",
                        TeacherId = values.Length>=5?values[4]:"",
                    });
                }
            }

            return list;
        }

        public static string GetDefaultPassword()
        {
            return ReadConfig("defaultPwd","");
        }

        public static void SetDefaultPassword(string defaultPwd)
        {
            WriteConfig("defaultPwd", defaultPwd);
        }

        public static string GetDefaultInviteCode()
        {
            return ReadConfig("defaultInviteCode", "");
        }

        public static void SetDefaultInviteCode(string defaultInviteCode)
        {
            WriteConfig("defaultInviteCode", defaultInviteCode);
        }

        public static string ReadConfig(string key, string defaultVal)
        {
            if (!ConfigurationManager.AppSettings.AllKeys.Contains(key))
                return defaultVal;

            return ConfigurationManager.AppSettings[key];
        }

        public static void WriteConfig(string key, string value)
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (!config.AppSettings.Settings.AllKeys.Contains(key))
            {
                config.AppSettings.Settings.Add(key,value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }
        }
    }
}
